use RabbitsWare

drop table [TweetApolloManager]
drop table [TweetApolloOperatorCustomer]
drop table [TweetApolloCompanyOperator]
drop table [TweetApolloCustomer]
drop table [TweetApolloCompany]

IF NOT EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = object_id(N'dbo.TweetApolloManager') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[TweetApolloManager](
	[TweetApolloUserName] varchar(128),
	[LastProcessedMentionId] int
)
GO

IF NOT EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = object_id(N'dbo.TweetApolloCompany') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[TweetApolloCompany](
	[CompanyId] int NOT NULL Primary Key,
	[Name] varchar(128),
	[Credits] int,
	[UserName] varchar(128),
	[PasswordHash] varchar (40) NOT NULL,
	[Email] varchar(128) NOT NULL,
	[Confirmed] bit default(0)
)
GO

IF NOT EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = object_id(N'dbo.TweetApolloCompanyOperator') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[TweetApolloCompanyOperator](
	[Company] int NOT NULL,
	[OperatorId] int NOT NULL PRIMARY KEY,
	[Name] varchar(128),
	[IsBusy] bit  default(0),
	CONSTRAINT [FK_Company] FOREIGN KEY ([Company]) REFERENCES [TweetApolloCompany]([CompanyId])
)
GO

IF NOT EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = object_id(N'dbo.TweetApolloCustomer') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[TweetApolloCustomer](
	[CustomerId] int NOT NULL PRIMARY KEY,
	[Name] varchar(128)
)
GO

IF NOT EXISTS (SELECT 1 FROM dbo.sysobjects WHERE id = object_id(N'dbo.TweetApolloOperatorCustomer') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [dbo].[TweetApolloOperatorCustomer](
	[OperatorId] int NOT NULL Primary Key,
	[CustomerId] int NOT NULL,
	CONSTRAINT [FK_OperatorId] FOREIGN KEY ([OperatorId]) REFERENCES [TweetApolloCompanyOperator]([OperatorId]),
	CONSTRAINT [FK_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [TweetApolloCustomer]([CustomerId])
)
GO
